package com.shg.web.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.shg.bean.Admin;
import com.shg.bean.PageView;
import com.shg.bean.Power;
import com.shg.bean.Roles;
import com.shg.service.PowerService;
import com.shg.service.RolesService;
import com.shg.web.tools.WebTool;

@Controller
public class RoleAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource RolesService rolesService;
	@Resource PowerService powerService;
	//权限列表
	private List<Power> powers;
	//导航
	private Map<String, Set<Power>> map;
	//
	private PageView<Roles> pageView;
	//
	private int maxRecord=3;
	//
	private int currentPage=1;
	//角色
	private Roles role;
	//
	private String message;
	//
	private long[] powerid;
	//用户ID
	private long[] adminid;
	//
	private List<Admin> admins;
	
 	public String index(){
 		try {
			map = powerService.getPower(WebTool.getAdmin());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "index";
	}
 	/**
 	 * 所有的角色查询
 	 * @return
 	 */
 	public String allRole(){
 		pageView=new PageView<Roles>(maxRecord, currentPage);
 		try {
			pageView.setQueryResult(rolesService.getAllRole(pageView.getFirstResult(), pageView.getMaxrecord()));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
 		return "allRole";
 	}
 	/**
 	 * 查询指定角色下的权限
 	 * @return
 	 */
 	public String powerList(){
 		try {
			role.setName(URLDecoder.decode(role.getName(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
 		try {
			powers = rolesService.getPowerList(role.getId());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
 		return "powerList";
 	}
 	/**
 	 * 更新角色的权限
 	 * @return
 	 */
 	public String updatePower(){
		try {
			rolesService.updatePower(powerid,role.getId());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
 		this.message="操作成功";
 		powerid=null;
 		return SUCCESS;
 	}
 	
 	/**
 	 * 查询指定角色下的用户
 	 * @return
 	 */
 	public String adminList(){
 		try {
			role.setName(URLDecoder.decode(role.getName(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
 		try {
			admins = rolesService.getAdminList(role.getId());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
 		return "adminList";
 	}
 	/**
 	 * 更新角色用户
 	 * @return
 	 */
 	public String updateAdmin(){
 		try {
			rolesService.updateRoleAdmin(role.getId(),adminid);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
 		adminid=null;
 		this.message="更新角色用户成功";
 		return SUCCESS;
 	}
 	/**
 	 * 角色解锁
 	 * @return
 	 */
 	public String  lockRole(){
 		pageView=new PageView<Roles>(maxRecord, currentPage);
 		try {
			pageView.setQueryResult(rolesService.updateLockRole(role,pageView.getFirstResult(),pageView.getMaxrecord()));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
 		return "allRole";
 	}
 	/**
 	 * 修改角色
 	 * @return
 	 */
 	public String editRole(){
 		try {
 			role.setRemarks(URLDecoder.decode(role.getRemarks(), "UTF-8"));
			role.setName(URLDecoder.decode(role.getName(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		try {
			rolesService.updateRole(role);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
 		return allRole();
 	}

 	public List<Power> getPowers() {
		return powers;
	}

	public void setPowers(List<Power> powers) {
		this.powers = powers;
	}
	public PageView<Roles> getPageView() {
		return pageView;
	}
	public void setPageView(PageView<Roles> pageView) {
		this.pageView = pageView;
	}
	public int getMaxRecord() {
		return maxRecord;
	}
	public void setMaxRecord(int maxRecord) {
		this.maxRecord = maxRecord;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public Roles getRole() {
		return role;
	}
	public void setRole(Roles role) {
		this.role = role;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public List<Admin> getAdmins() {
		return admins;
	}
	public void setAdmins(List<Admin> admins) {
		this.admins = admins;
	}
	public long[] getAdminid() {
		return adminid;
	}
	public void setAdminid(long[] adminid) {
		this.adminid = adminid;
	}
	public long[] getPowerid() {
		return powerid;
	}
	public void setPowerid(long[] powerid) {
		this.powerid = powerid;
	}
	public Map<String, Set<Power>> getMap() {
		return map;
	}
	public void setMap(Map<String, Set<Power>> map) {
		this.map = map;
	}
}
